<?php
//用户管理
namespace Home\Controller;

use Think\Controller;

class  UseradminController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['username'])) {


            $this->error('页面加载中。。。', U('Login/Login'), 1);

        }
        if(session('qx')!=1){
            $this->error('您没有权限访问');
        }
        $table1="useradmin";
        $this->assign('table',$table1);
    }

    public function Userinfo()
    {


        $User = M('sysuser');
        $us = $User->select();
        $this->assign('us', $us);
        $this->display();


    }

    public function Adduser()
    {

        if (IS_AJAX) {

            $User = M('sysuser');
            $cf['username'] = I('post.username');
            $tid = $User->where($cf)->find();
            if ($tid) {

                $data = 3;
            } else {


                $data['username'] = I('post.username');

                $data['password'] = MD5(I('post.password'));
                $data['tk_qx'] = I('post.tk_qx');

                $add = $User->add($data);
                if ($add) {

                    $data = 0;
                } else {
                    $data = 1;
                }
            }

            echo $data;
        } else {
            $this->display();
        }

    }

    public function Userxg()
    {
        $User = M('Sysuser');


        if (IS_AJAX) {
            $data['id'] = I('post.id');

            $data['username'] = I('post.username');


            if (!empty($_POST['password'])) {
                $data['password'] = MD5(I('post.password'));
            }
            $data['tk_qx'] = I('post.tk_qx');
            $updata = $User->save($data); // 根据条件保存修改的数据
            if ($updata) {

                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;

        } else {

            $map['id'] = I('get.id');
            $us = $User->where($map)->find();
            $this->assign('us', $us);
            $this->display();

        }


    }


    public function del()
    {

        $map['id'] = I('get.id');

        $User = M('Sysuser');

        $del = $User->where($map)->delete();
        if ($del) {

            $this->success('删除成功');

        } else {

            $this->error('删除失败');

        }


    }

    //put your code here
}

